﻿using System;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Data;
using System.Xml;
using System.Collections.Generic;
using System.Text;

namespace XsltDbClient
{
    public class RichXmlClient
    {
        string url, username, password;

        public RichXmlClient(string url, string username, string password)
        {
            this.url = url;
            this.username = username;
            this.password = password;
        }
        public DataTable GetXsltDbList()
        {
            string input = createXml(
                new XmlParam("function", "GetXsltDbList"),
                new XmlParam("username", username),
                new XmlParam("password", password));
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(invoke(input));
            DataTable dt = new DataTable();
            dt.ReadXml(new StringReader(doc.SelectSingleNode("//list").InnerText));
            return dt;
        }

        public string GetXsltDb(int ModuleID)
        {
            string input = createXml(
                new XmlParam("function", "GetXsltDb"),
                new XmlParam("ModuleID", ModuleID.ToString()),
                new XmlParam("username", username),
                new XmlParam("password", password));
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(invoke(input));
            return doc.SelectSingleNode("//xslt").InnerText;
        }

        public void PublishAll()
        {
            string input = createXml(
                new XmlParam("function", "PublishAll"),
                new XmlParam("username", username),
                new XmlParam("password", password));
            invoke(input);
        }

        public void PublishModule(int ModuleID)
        {
            string input = createXml(
                new XmlParam("function", "Publish"),
                new XmlParam("ModuleID", ModuleID.ToString()),
                new XmlParam("username", username),
                new XmlParam("password", password));
            invoke(input);
        }

        public void SaveXsltDb(int ModuleID, string xslt)
        {
            string input = createXml(
                new XmlParam("function", "SaveXsltDb"),
                new XmlParam("ModuleID", ModuleID.ToString()),
                new XmlParam("xslt", xslt),
                new XmlParam("username", username),
                new XmlParam("password", password));
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(invoke(input));
        }

        private string createXml(params XmlParam[] prms)
        {
            XmlDocument doc = new XmlDocument();

            XmlElement root = doc.CreateElement("root");
            foreach(XmlParam p in prms)
            {
                XmlElement e = doc.CreateElement(p.name);
                e.InnerText = p.value;
                root.AppendChild(e);
            }

            doc.AppendChild(root);
            return doc.OuterXml;
        }

        private string invoke(string input)
        {
            WebRequest r = WebRequest.Create(url);
            byte[] prm = Encoding.UTF8.GetBytes(input);
            r.ContentType = "text/plain";
            r.ContentLength = prm.Length;
            r.Method = "POST";
            Stream rs = r.GetRequestStream();
            rs.Write(prm,0,prm.Length);
                
            using (StreamReader sr = new StreamReader(r.GetResponse().GetResponseStream()))
                return sr.ReadToEnd();
        }
        private class XmlParam
        {
            public string name;
            public string value;

            public XmlParam(string name, string value)
            {
                this.value = value;
                this.name = name;
            }
        }

        public enum PingResult
        {
            OK,
            PWD,
            VER,
            ERR
        }
        public PingResult Ping()
        {
            try
            {
                string input = createXml(
                    new XmlParam("function", "GetProtocolVersion"),
                    new XmlParam("username", username),
                    new XmlParam("password", password));
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(invoke(input));
                if (doc.SelectSingleNode("//result").InnerText == "ok")
                {
                    if ( doc.SelectSingleNode("//version").InnerText != "3" )
                        return PingResult.VER;
                    return PingResult.OK;
                }
                return PingResult.PWD;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return PingResult.ERR;
            }
        }
    }

}
